home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap06 / FoneList / FoneList.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  4.6 KB  |  182 lines

  1. //***********************************************************************
  2. //
  3. //  FoneList.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include <afxext.h>
  9. #include "Resource.h"
  10. #include "FoneList.h"
  11.  
  12. CMyApp myApp;
  13.  
  14. /////////////////////////////////////////////////////////////////////////
  15. // CMyApp member functions
  16.  
  17. BOOL CMyApp::InitInstance ()
  18. {
  19.     m_pMainWnd = new CMainWindow;
  20.     m_pMainWnd->ShowWindow (m_nCmdShow);
  21.     m_pMainWnd->UpdateWindow ();
  22.     return TRUE;
  23. }
  24.  
  25. /////////////////////////////////////////////////////////////////////////
  26. // CMainWindow message map and member functions
  27.  
  28. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  29.     ON_WM_CREATE ()
  30.     ON_WM_SIZE ()
  31.     ON_COMMAND (IDM_OPTIONS_NEW, OnOptionsNew)
  32.     ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
  33. END_MESSAGE_MAP ()
  34.  
  35. CMainWindow::CMainWindow ()
  36. {
  37.     Create (NULL, "Names and Phones", WS_OVERLAPPEDWINDOW, rectDefault,
  38.         NULL, MAKEINTRESOURCE (IDR_MAINFRAME));
  39. }
  40.  
  41. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  42. {
  43.     if (CFrameWnd::OnCreate (lpcs) == -1)
  44.         return -1;
  45.  
  46.     m_ctlListBox.Create (WS_CHILD | WS_VISIBLE, CRect (0, 0, 0, 0),
  47.         this, IDC_LISTBOX);
  48.     return 0;
  49. }
  50.  
  51. void CMainWindow::OnSize (UINT nType, int cx, int cy)
  52. {
  53.     m_ctlListBox.SetWindowPos (NULL, 0, 0, cx, cy,
  54.         SWP_NOMOVE | SWP_NOZORDER);
  55. }
  56.  
  57. void CMainWindow::OnOptionsNew ()
  58. {
  59.     m_ctlListBox.PromptForNewEntry ();
  60. }
  61.  
  62. void CMainWindow::OnOptionsExit ()
  63. {
  64.     SendMessage (WM_CLOSE, 0, 0);
  65. }
  66.  
  67. /////////////////////////////////////////////////////////////////////////
  68. // CEditDialog message map and member functions
  69.  
  70. BEGIN_MESSAGE_MAP (CEditDialog, CDialog)
  71.     ON_EN_CHANGE (IDC_NAME, OnUpdateOKButton)
  72. END_MESSAGE_MAP ()
  73.  
  74. BOOL CEditDialog::OnInitDialog ()
  75. {
  76.     CDialog::OnInitDialog ();
  77.  
  78.     m_ctlOKButton.AutoLoad (IDOK, this);
  79.     m_ctlCancelButton.AutoLoad (IDCANCEL, this);
  80.     m_ctlPhone.SubclassDlgItem (IDC_PHONE, this);
  81.  
  82.     OnUpdateOKButton ();
  83.     return TRUE;
  84. }
  85.  
  86. void CEditDialog::OnUpdateOKButton ()
  87. {
  88.     CEdit* pCtlName = (CEdit*) GetDlgItem (IDC_NAME);
  89.     m_ctlOKButton.EnableWindow (pCtlName->LineLength ());
  90. }
  91.  
  92. void CEditDialog::DoDataExchange (CDataExchange* pDX)
  93. {
  94.     CDialog::DoDataExchange (pDX);
  95.  
  96.     DDX_Text (pDX, IDC_NAME, m_strName);
  97.     DDV_MaxChars (pDX, m_strName, 32);
  98.     DDX_Text (pDX, IDC_PHONE, m_strPhone);
  99.     DDV_MaxChars (pDX, m_strPhone, 32);
  100. }
  101.  
  102. /////////////////////////////////////////////////////////////////////////
  103. // CFoneListBox message map and member functions
  104.  
  105. BEGIN_MESSAGE_MAP (CFoneListBox, CListBox)
  106.     ON_WM_CREATE ()
  107.     ON_CONTROL_REFLECT (LBN_DBLCLK, OnEditItem)
  108. END_MESSAGE_MAP ()
  109.  
  110. BOOL CFoneListBox::PreCreateWindow (CREATESTRUCT& cs)
  111. {
  112.     if (!CListBox::PreCreateWindow (cs))
  113.         return FALSE;
  114.  
  115.     cs.style |= LBS_SORT | LBS_USETABSTOPS | LBS_NOTIFY |
  116.         LBS_NOINTEGRALHEIGHT;
  117.     return TRUE;
  118. }
  119.  
  120. int CFoneListBox::OnCreate (LPCREATESTRUCT lpcs)
  121. {
  122.     if (CListBox::OnCreate (lpcs) == -1)
  123.         return -1;
  124.  
  125.     CClientDC dc (this);
  126.     int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
  127.  
  128.     m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
  129.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  130.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  131.     SetFont (&m_font, FALSE);
  132.  
  133.     SetTabStops (128);
  134.     return 0;
  135. }
  136.  
  137. void CFoneListBox::OnEditItem ()
  138. {
  139.     CEditDialog dlg;
  140.  
  141.     CString strItem;
  142.     int nIndex = GetCurSel ();
  143.     GetText (nIndex, strItem);
  144.     int nPos = strItem.Find ('\t');
  145.  
  146.     dlg.m_strName = strItem.Left (nPos);
  147.     dlg.m_strPhone = strItem.Right (strItem.GetLength () - nPos - 1);
  148.  
  149.     if (dlg.DoModal () == IDOK) {
  150.         strItem = dlg.m_strName + "\t" + dlg.m_strPhone;
  151.         DeleteString (nIndex);
  152.         AddString (strItem);
  153.     }
  154.     SetFocus ();
  155. }
  156.  
  157. void CFoneListBox::PromptForNewEntry ()
  158. {
  159.     CEditDialog dlg;
  160.     if (dlg.DoModal () == IDOK) {
  161.         CString strItem = dlg.m_strName + "\t" + dlg.m_strPhone;
  162.         AddString (strItem);
  163.     }
  164.     SetFocus ();
  165. }
  166.  
  167. /////////////////////////////////////////////////////////////////////////
  168. // CNumEdit message map and member functions
  169.  
  170. BEGIN_MESSAGE_MAP (CNumEdit, CEdit)
  171.     ON_WM_CHAR ()
  172. END_MESSAGE_MAP ()
  173.  
  174. void CNumEdit::OnChar (UINT nChar, UINT nRepCnt, UINT nFlags)
  175. {
  176.     if (((nChar >= '0') && (nChar <= '9')) ||
  177.         (nChar == VK_BACK) || (nChar == '(') || (nChar == ')') ||
  178.         (nChar == '-') || (nChar == ' '))
  179.  
  180.         CEdit::OnChar (nChar, nRepCnt, nFlags);
  181. }
  182.